package main

import (
	"fmt"
	"time"
)

/**
无缓冲的channel在发送和接受消息时都是阻塞的,
执行结果是
len(ch) = 0, cap(ch) = 0
子协程：i = 0
num = 0
子协程：i = 1
子协程：i = 2
num = 1
num = 2
发送一个接收一个，可能顺序不是很符合，但就是先发送一个再接收一个
 */
func main()  {
	ch := make(chan int)

	fmt.Printf("len(ch) = %d, cap(ch) = %d\n", len(ch), cap(ch))

	go func() {
		for i := 0; i < 3; i++ {
			fmt.Printf("子协程：i = %d\n", i)
			ch <- i
		}
	}()

	time.Sleep(time.Second * 2)

	for i := 0; i < 3; i++ {
		num := <- ch
		fmt.Println("num =", num)
	}
}
